# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import codecs
import json
from  scrapy.pipelines.images import  ImagesPipeline
# import MySQLdb

class ArticlespiderPipeline(object):
    def process_item(self, item, spider):
        return item

class JsonWithEncodingPipline(object):
    def __init__(self):
        self.file = codecs.open('article.json','w',encoding='utf-8')
    def process_item(self,item,spider):
        lines = json.dumps(dict(item),ensure_ascii=False)
        self.file.write(lines)
        return  item
    def spider_closed(self,spider):
        self.file.close()
#
# class MysqlPipline(object):
#     def __init__(self):
#         #self.conn = MySQLdb.connect('127.0.0.1','root','root','python',charset='utf-8')
#         self.conn = MySQLdb.connect("localhost", "root", "root", "python", charset='utf8')
#         self.cursor = self.conn.cursor()
#     def process_item(self,item,spider):
#         insert_sql = """
#                 insert into article(title ,url ,create_date, fav_nums) values(%s,%s,%s,%s)
#         """
#         self.cursor.execute(insert_sql,(item['title'],item['url'],item['create_date'],item['fav_nums']))
#         self.conn.commit()
#
# class StockMysqlPipline(object):
#     def __init__(self):
#
#         #self.conn = MySQLdb.connect('127.0.0.1','root','root','python',charset='utf-8')
#         self.conn = MySQLdb.connect("localhost", "root", "root", "python", charset='utf8')
#         self.cursor = self.conn.cursor()
#     def process_item(self,item,spider):
#
#         insert_sql = """
#                 insert into stock(code ,name) values(%s,%s)
#         """
#         self.cursor.execute(insert_sql,(item['code'],item['name']))
#         self.conn.commit()

 #函数重载
class ArticleImagePipeline(ImagesPipeline):
    def item_completed(self, results, item, info):
        if "headimg" in item:
            for ok,value in results:
                headimg = value["path"]
            item["headimg"] = headimg
        return  item